package com.seuic.hnjiningposapp.hnapps.ui.view.dialog;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.seuic.hnjiningposapp.R;
import com.seuic.hnjiningposapp.hnapps.bean.StatisticsBean;
import com.seuic.hnjiningposapp.hnapps.util.GetMoneyLvUtils;
import com.seuic.hnjiningposapp.hnapps.util.SelTimePickerUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 业务统计
 */
public class StatisticsPp extends BasePopupView {
    Unbinder bind;
    @BindView(R.id.sel_op_type)
    TextView selOpType;
    @BindView(R.id.query_btn)
    Button queryBtn;
    @BindView(R.id.start_time)
    TextView startTime;
    @BindView(R.id.end_time)
    TextView endTime;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    Context mConntext;
    @BindView(R.id.printbtn)
    Button printbtn;
    @BindView(R.id.title)
    TextView title;
    private Adapter adapter;
    private List<StatisticsBean> beans;
    onStatisPrint mOnStatisPrint;

    String StartTime;
    String EndTime;
    String status;
    queryStataDeal mQueryStataDeal;

    public StatisticsPp(@NonNull Context context, onStatisPrint onStatisPrint, queryStataDeal queryStataDeal) {
        super(context);
        this.mConntext = context;
        this.mOnStatisPrint = onStatisPrint;
        mQueryStataDeal = queryStataDeal;
    }

    @Override
    protected int getPopupLayoutId() {
        return R.layout.statistics_pp;
    }

    public void setBeans(List<StatisticsBean> beans) {
        this.beans = beans;
    }

    public void setStartTime(String startTime) {
        StartTime = startTime;
    }

    public void setEndTime(String endTime) {
        EndTime = endTime;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        bind = ButterKnife.bind(this);
        title.setText("本机交易汇总查询");
        if (status.equals("")) {
            selOpType.setText("所有交易");
        } else if (status.equals("S")) {
            selOpType.setText("正常交易");
        } else if (status.equals("F")) {
            selOpType.setText("异常交易");
        }
        startTime.setText(StartTime);
        endTime.setText(EndTime);

        recyclerView.setLayoutManager(new LinearLayoutManager(mConntext));
        adapter = new Adapter(R.layout.statistics_pp_item_layout, null);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(mConntext, DividerItemDecoration.VERTICAL));
        adapter.setNewData(beans);
    }

    @OnClick({R.id.printbtn, R.id.sel_op_type, R.id.query_btn, R.id.start_time, R.id.end_time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.sel_op_type:
                //选择类型
                String types[] = new String[]{"所有交易", "正常交易", "异常交易"};
                new XPopup.Builder(mConntext)
                        .asBottomList("", types, (position, text) -> {
                            selOpType.setText(text);
                        }).show();
                break;
            case R.id.query_btn:
                //开始查询
                String statuss = "";
                if (selOpType.getText().toString().equals("正常交易")) {
                    statuss = "S";
                } else if (selOpType.getText().toString().equals("异常交易")) {
                    statuss = "F";
                }
                mQueryStataDeal.queryStatData(startTime.getText().toString(), endTime.getText().toString(), statuss);
                break;
            case R.id.start_time:
                //开始时间
                SelTimePickerUtil.loadData(mConntext, data ->
                        startTime.setText(data), "yyyyMMdd", true, true, true, false, false, false);
                SelTimePickerUtil.show();
                break;
            case R.id.end_time:
                //结束时间
                SelTimePickerUtil.loadData(mConntext, data ->
                        endTime.setText(data), "yyyyMMdd", true, true, true, false, false, false);
                SelTimePickerUtil.show();
                break;
            case R.id.printbtn:
                mOnStatisPrint.onStatisPrintto(beans);
                break;
        }
    }


    private class Adapter extends BaseQuickAdapter<StatisticsBean, BaseViewHolder> {
        public Adapter(int layoutResId, List<StatisticsBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder baseViewHolder, StatisticsBean bean) {
            TextView num_tv, item_name, item_type, item_pennum, item_money, device_number, item_time, end_time;
            num_tv = baseViewHolder.findView(R.id.num_tv);//序号
            item_name = baseViewHolder.findView(R.id.item_name);//操作名称
            item_type = baseViewHolder.findView(R.id.item_type);//交易状态
            item_pennum = baseViewHolder.findView(R.id.item_pennum);//交易笔数
            item_money = baseViewHolder.findView(R.id.item_money);//金额
            device_number = baseViewHolder.findView(R.id.device_number);//设备号
            item_time = baseViewHolder.findView(R.id.item_time);//时间

            num_tv.setText("序号: " + (baseViewHolder.getPosition() + 1));

            if (bean.getTradeType().equals("S")) {
                item_type.setText("正常");
            } else if (bean.getTradeType().equals("F")) {
                item_type.setText("异常");
            }
            item_name.setText(bean.getTradename());


            item_pennum.setText(bean.getTransnum());

            if (bean.getAmount()==null){
                item_money.setText("0.00");
            }else {
                if (bean.getAmount().equals("")){
                    item_money.setText("0.00");
                }else {
                    item_money.setText(bean.getAmount());

                }
            }


            device_number.setText(bean.getDevNum());
            item_time.setText(GetMoneyLvUtils.gettimeNohour(bean.getStatisdate()));
        }
    }

    public interface onStatisPrint {
        void onStatisPrintto(List<StatisticsBean> beans);
    }

    public interface queryStataDeal {
        void queryStatData(String starttime, String endtime, String status);
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
        //注解解绑
        if (bind != null && bind != Unbinder.EMPTY) {
            bind.unbind();
        }
        this.bind = null;
    }
}
